package com.dkts.oil.service.impl.address;

import com.dkts.oil.exception.OilException;
import com.dkts.oil.service.impl.AddressPlanV2ServiceImpl;
import com.dkts.oil.web.vo.request.SiteSelectionRequest;
import com.dkts.oil.web.vo.request.subReq.IndexGeoMountain;
import com.dkts.oil.web.vo.response.AddressPlanResponse;
import org.locationtech.jts.geom.Geometry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CbkSitesHelper  {

    @Qualifier("addressPlanV2ServiceImpl")
    @Autowired
    AddressPlanV2ServiceImpl addressPlanV2Service;
    public List<AddressPlanResponse.Site> addressPlan(SiteSelectionRequest request, Geometry planGeometry, List<Geometry> areaNotAirport, List<Geometry> obstacles, String name) {
        if (areaNotAirport.isEmpty()) {
            throw new OilException("规划区域无可用点放置储备库");
        }
        List<IndexGeoMountain> getMountainIndex = addressPlanV2Service.getMountainIndex(planGeometry, areaNotAirport, request.point);
        List<AddressPlanResponse.Site> wcgySites = new ArrayList<>();
        List<Geometry> availablePoints = new ArrayList<>();
        for (int i = 0; i < getMountainIndex.size(); i++) {
            IndexGeoMountain pair = getMountainIndex.get(i);
            AddressPlanResponse.Site site = addressPlanV2Service.pickSiteFromGrid(
                    availablePoints,
                    request.getAreaConfig().getFuelSupplyCenterArea(),
                    name,
                    request.getCenterPoint(),
                    pair.geometry,
                    i + 1, request.getPoint()
            );
            wcgySites.add(site);
            if (wcgySites.size() > 3) {
                break;
            }
        }
        return wcgySites;

    }
}
